using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Xml.Serialization;
using System.Reflection;
using System.IO;
using System.Xml.Schema;
using System.Xml;
using System.Globalization;
using Barbar.MapReport.Common;

namespace Barbar.MapReport.HtmlRender.Shapes {
  internal class DoubleCircleWithNumberShape : DoubleCircleShape, IShape {
    private int m_Number;

    public DoubleCircleWithNumberShape() {
    }

    public DoubleCircleWithNumberShape(int radius, Color leftColor, Color rightColor, int number) : base(radius, leftColor, rightColor) {
      m_Number = number;
    }

    public int Number {
      get { return m_Number; }
      set { m_Number = value; }
    }

    public override void Draw(System.Drawing.Graphics graphics) {
      Assembly assembly = Assembly.GetExecutingAssembly();
      Bitmap bitmap = null;
      using (Stream stream = assembly.GetManifestResourceStream("Barbar.MapReport.Core.Resources.DoubleCircleWithNumber56.png")) {
        bitmap = (Bitmap)Bitmap.FromStream(stream);
        stream.Close();
      }

      graphics.FillPie(new SolidBrush(LeftColor), Rectangle, 90.0f, 180.0f);
      graphics.FillPie(new SolidBrush(RightColor), Rectangle, 270.0f, 180.0f);
      graphics.DrawImage(bitmap, 0, 0);

      Rectangle innerRect = new Rectangle(Width / 2 - Width / 6, Width / 2 - Width / 6, Width / 3, Width / 3);
      graphics.FillEllipse(new SolidBrush(Color.FromArgb(100, Color.Black)), innerRect);
      
      graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
      Font font = new Font(new FontFamily("Arial"), Width / 6, FontStyle.Bold);
      SizeF labelSize = graphics.MeasureString(Convert.ToString(m_Number, CultureInfo.InvariantCulture), font);

      graphics.DrawString(Convert.ToString(m_Number, CultureInfo.InvariantCulture), font, new SolidBrush(Color.White), Width / 2 - labelSize.Width / 2 + 1, Width / 2 - labelSize.Height / 2 + 1);
    }

    #region IXmlSerializable Members

    public override void ReadXml(XmlReader reader) {
      reader.Read();
      if (reader.NodeType != XmlNodeType.Element || reader.Value == "dcns")
        throw new MapReportException("Excepted 'dcns'");
      base.DeserializeAttributes(reader);
      Number = XmlConvert.ToInt32(reader.GetAttribute("n"));
    }

    public override void WriteXml(XmlWriter writer) {
      writer.WriteStartElement("dcns");
      base.SerializeAttributes(writer);
      writer.WriteAttributeString("n", XmlConvert.ToString(Number));
      writer.WriteEndElement();
    }

    #endregion
  }
}
